package com.woniuxy.controller;


import com.github.pagehelper.PageInfo;
import com.woniuxy.annotation.MyLog;
import com.woniuxy.annotation.RequirePermission;
import com.woniuxy.entity.Dept;
import com.woniuxy.entity.Home;
import com.woniuxy.entity.Hospital;
import com.woniuxy.service.hospital.DeptService;
import com.woniuxy.service.hospital.HomeService;
import com.woniuxy.service.hospital.HospitalService;
import com.woniuxy.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class HomeController {
    @Autowired
    private HomeService homeService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DeptService deptService;


    @RequirePermission("sickroom:find")
    @PostMapping("sickroom/findAll")
    public ResultVo findAll(@RequestBody Map<String,String> map){

        String strPage =map.remove("page");
        String strSize =map.remove("size");
        PageInfo<Home> homes = homeService.findAll(Integer.valueOf(strPage),Integer.valueOf(strSize));
        return ResultVo.success(homes);
    }
    @MyLog("添加病房")
    @RequirePermission("sickroom:save")
    @PostMapping("sickroom/save")
    public ResultVo save(@RequestBody Home home){
        int rows = homeService.save(home);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    @MyLog("修改病房信息")
    @RequirePermission("sickroom:update")
    @PostMapping("sickroom/update")
    public ResultVo update(@RequestBody Home home) {
        int rows = homeService.update(home);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }



    @PostMapping("sickroom/findAllHospital")
    public ResultVo findAllHospital() {
        List<Hospital> list = hospitalService.findAllHospital();
        System.out.println(list);
        return ResultVo.success(list);
    }

    @PostMapping("sickroom/findAllDept")
    public ResultVo findAllDept(@RequestBody String hid) {

        List<Dept> list = deptService.findAllDept(Integer.valueOf(hid));
        System.out.println(list);
        return ResultVo.success(list);

    }


    @MyLog("删除病房")
    @RequirePermission("sickroom:delete")
    @PostMapping("sickroom/delete")
    public ResultVo delete(@RequestBody String hId) {

        if(!StringUtils.hasText(hId)){
            throw new RuntimeException("hId is required");
        }
        int rows = homeService.delete(Integer.valueOf(hId));
        return rows > 0 ? ResultVo.success() : ResultVo.failure();

    }

    @RequirePermission("sickroom:find")
    @PostMapping("sickroom/find")
    public ResultVo find(@RequestBody Map<String,String> map) {
        System.out.println(map);
        Dept dept = new Dept();
        String hId = map.remove("hId");
        dept.sethId(Integer.valueOf(hId));

        System.out.println(map.size());
        if (map.size()>3) {
            String deptId = map.remove("deptId");
            dept.setDeptId(Integer.valueOf(deptId));
        }
        String page = map.remove("page");
        String size = map.remove("size");
        System.out.println(page+ "   ," + size);
        System.out.println(dept.toString());
        PageInfo<Home> list = homeService.findSelect(dept,Integer.valueOf(page),Integer.valueOf(size));
        return ResultVo.success(list);
    }
}
